﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TCHSHue.DAL;
namespace TCHSHue.BLL
{
    public class PhuongBLL
    {
        public static List<Phuong> getList()
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = from e in data.Phuongs
                         orderby e.tenPhuong ascending
                         select e;
                return ls.ToList();
            }
        }
        public static bool create(string tenPhuong)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = (from e in data.Phuongs
                          where e.tenPhuong == tenPhuong
                          select e).SingleOrDefault();
                if (ls != null || string.IsNullOrEmpty(tenPhuong))
                    return false;
                Phuong phuong = new Phuong { tenPhuong = tenPhuong };
                data.Phuongs.InsertOnSubmit(phuong);
                data.SubmitChanges();
                return true;
            }
        }

        internal static bool delete(string maPhuong)
        {
            int temp = 0;
            if (int.TryParse(maPhuong, out temp))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var phuong = (from e in data.Phuongs
                                  where e.maPhuong == int.Parse(maPhuong)
                                  select e).SingleOrDefault();
                    if (phuong != null)
                    {
                        data.Phuongs.DeleteOnSubmit(phuong);
                        data.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            else
                return false;
        }

        internal static bool update(string maPhuong, string tenPhuong)
        {
            int temp = 0;
            if (int.TryParse(maPhuong, out temp))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var phuong = (from e in data.Phuongs
                                  where e.maPhuong == int.Parse(maPhuong)
                                  select e).SingleOrDefault();
                    if (phuong != null)
                    {
                        phuong.tenPhuong = tenPhuong;
                        data.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            else
                return false;
        }

        internal static Phuong getPhuong(string maPhuong)
        {
            int temp = 0;
            if (int.TryParse(maPhuong, out temp))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var phuong = (from e in data.Phuongs
                                  where e.maPhuong == int.Parse(maPhuong)
                                  select e).SingleOrDefault();
                    return phuong;
                }
            }
            return null;
        }
    }
}